<!--
 * @Author: HeAo
 * @Date: 2021-10-11 16:12:32
 * @LastEditTime: 2021-10-11 16:42:53
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day03\3-组件通信-子传父.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue组件传参-子传父</title>
  <script src="../js/vue.js"></script>
  <style>
    .box {
      width: 50px;
      height: 50px;
      border: 1px solid teal;
    }
  </style>
</head>

<body>
  <div id="app">
    <h2>父组件</h2>
    <div class="box">
      {{parentData}}
    </div>
    <my-com v-on:child-event="parentHandler">
      成功按钮
    </my-com>
  </div>
  <script>
    let component1 = {
      data() {
        return {
          childData: 'Windows 11'
        }
      },
      methods: {
        toEmitHandler() {
          // $emit方法可以向上发送事件，并且可以携带数据
          this.$emit('child-event', this.childData)
        }
      },
      template: `
        <div>
          <h3>子组件</h3>
          <button @click="toEmitHandler">发送事件</button>
          <button>
            <slot></slot>
          </button>
        </div>
      `
    }

    let vm = new Vue({
      el: '#app',
      data: {
        parentData: 'window98'
      },
      methods: {
        parentHandler(data) {
          console.log('你说的我知道了', data);
          // 修改父组件数据，由父组件自己来决定
          this.parentData = data
        }
      },
      components: {
        'my-com': component1
      }
    })
  </script>
</body>

</html>